﻿<%@ WebHandler Language="C#" Class="BookingHandler" %>

using System;
using System.Web;

public class BookingHandler : IHttpHandler
{
    private string Channel = "LHJ";
    private string Key = "EF1543A5EEEF106871A80A91927FE04E1E4FBE70";
    private string Url = "http://www.mangocity.com/HDL/service/MZExHotel";
    private ResultRes result = null;
    private System.Collections.Generic.List<SampleHotel> hotelList = null;
    private MutilHotelResponse mHotelData = null;
    public void ProcessRequest(HttpContext context)
    {
        context.Response.ContentType = "text/plain";
        string postXml = string.Empty;
        string responseXml = string.Empty;
        switch (context.Request["method"])
        {
            case "1":
                postXml = getMultiSearchXML(context.Request.QueryString["citycode"], context.Request.QueryString["checkindate"], context.Request.QueryString["checkoutdate"]);
                responseXml = sendData(postXml).ToString();
                break;
            case "2":
                postXml = getSingleSearchXML(context.Request.QueryString["citycode"], context.Request.QueryString["hotelcode"], context.Request.QueryString["checkindate"], context.Request.QueryString["checkoutdate"]);
                responseXml = sendData(postXml).ToString();
                break;
            default:
                break;
        }
        context.Response.Write(responseXml);
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }

    private ResultRes GetResultObj(string xml)
    {
        System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(ResultRes));
        System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(new System.IO.MemoryStream(System.Text.Encoding.Default.GetBytes(xml)));
        return (ResultRes)(ser.Deserialize(reader));

    }

    private string getXMLHeader()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.AppendLine("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
        sb.AppendLine("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:dto=\"http://www.mangocity.com/hdl/ex/dto\">");
        sb.AppendLine("<soapenv:Header/>");
        sb.AppendLine("<soapenv:Body>");
        return sb.ToString();
    }

    private string getXMLFooter()
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.AppendLine("</soapenv:Body>");
        sb.AppendLine("</soapenv:Envelope>");
        return sb.ToString();
    }

    private string getMultiSearchXML(string cityCode, string checkInDate, string checkOutDate)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.AppendLine(getXMLHeader());
        sb.AppendLine("<dto:MutilHotelRequest>");
        sb.AppendLine("<dto:author>");
        sb.AppendLine("<dto:channel>" + this.Channel + "</dto:channel>");
        sb.AppendLine("<dto:key>" + this.Key + "</dto:key>");
        sb.AppendLine("<dto:timeStamp>" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "</dto:timeStamp>");
        sb.AppendLine("</dto:author>");
        sb.AppendLine("<dto:cityCode>" + cityCode + "</dto:cityCode>");
        sb.AppendLine("<dto:checkInDate>" + checkInDate + "</dto:checkInDate>");
        sb.AppendLine("<dto:checkOutDate>" + checkOutDate + "</dto:checkOutDate>");
        sb.AppendLine("</dto:MutilHotelRequest>");
        sb.AppendLine(getXMLFooter());
        return sb.ToString();
    }

    private string getSingleSearchXML(string cityCode, string hotelCode, string checkInDate, string checkOutDate)
    {
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        sb.AppendLine(getXMLHeader());
        sb.AppendLine("<dto:SingleHotelRequest>");
        sb.AppendLine("<dto:author>");
        sb.AppendLine("<dto:channel>" + this.Channel + "</dto:channel>");
        sb.AppendLine("<dto:key>" + this.Key + "</dto:key>");
        sb.AppendLine("<dto:timeStamp>" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss") + "</dto:timeStamp>");
        sb.AppendLine("</dto:author>");
        sb.AppendLine("<dto:cityCode>" + cityCode + "</dto:cityCode>");
        sb.AppendLine("<dto:hotelCode>" + hotelCode + "</dto:hotelCode>");
        sb.AppendLine("<dto:checkInDate>" + checkInDate + "</dto:checkInDate>");
        sb.AppendLine("<dto:checkOutDate>" + checkOutDate + "</dto:checkOutDate>");
        sb.AppendLine("</dto:SingleHotelRequest>");
        sb.AppendLine(getXMLFooter());
        return sb.ToString();
    }


    private bool sendData(string data)
    {
        try
        {
            //Our postvars
            byte[] buffer = System.Text.Encoding.ASCII.GetBytes(data);
            //Initialisation, we use localhost, change if appliable
            System.Net.HttpWebRequest WebReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(this.Url);
            //Our method is post, otherwise the buffer (postvars) would be useless
            WebReq.Method = "POST";
            //We use form contentType, for the postvars.
            WebReq.ContentType = "text/xml";
            //The length of the buffer (postvars) is used as contentlength.
            WebReq.ContentLength = buffer.Length;
            //We open a stream for writing the postvars
            System.IO.Stream postData = WebReq.GetRequestStream();
            //Now we write, and afterwards, we close. Closing is always important!
            postData.Write(buffer, 0, buffer.Length);
            postData.Close();
            //Get the response handle, we have no true response yet!
            System.Net.HttpWebResponse WebResp = (System.Net.HttpWebResponse)WebReq.GetResponse();
            //Let's show some information about the response
            //Console.WriteLine(WebResp.StatusCode);
            //Console.WriteLine(WebResp.Server);

            //Now, we read the response (the string), and output it.
            System.IO.Stream answer = WebResp.GetResponseStream();
            //System.IO.StreamReader reader = new System.IO.StreamReader(answer);
            //return reader.ReadToEnd();
            //System.Xml.Serialization.XmlSerializer ser = new System.Xml.Serialization.XmlSerializer(typeof(ResultRes));            
            //System.Xml.XmlTextReader reader = new System.Xml.XmlTextReader(answer);
            //result = (ResultRes)(ser.Deserialize(reader));

            //System.Xml.Serialization.XmlSerializer serList = new System.Xml.Serialization.XmlSerializer(typeof(System.Collections.Generic.List<SampleHotel>));
            //hotelList = (System.Collections.Generic.List<SampleHotel>)(serList.Deserialize(reader));

            //mHotelData = new SerializeHelper().FromSoap<MutilHotelResponse>(reader.ReadToEnd());

            //Congratulations, you just requested your first POST page, you
            //can now start logging into most login forms, with your application
            //Or other examples.

            string rtn = System.Runtime.Serialization.Json.JsonReaderWriterFactory.CreateJsonReader(answer, System.Xml.XmlDictionaryReaderQuotas.Max).Value;
            return true;
        }
        catch (Exception ex)
        { }
        return false;
    }        
}